<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/10
 * Time: 12:01
 */

namespace plugin\admin\app\logic\reward;


use plugin\admin\app\logic\BaseLogic;
use plugin\admin\app\model\reward\RewardPlanModel;
use support\Request;
use support\Response;

class RewardPlanLogic extends BaseLogic
{
    protected static $instance;

    public function __construct()
    {
        $this->model = new RewardPlanModel();
    }

    public static function getInstance()
    {
        //        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return new self();
    }

    public function select(Request $request): Response
    {
        $field = $request->input('field');
        if($field==false){
            $request->inputSet('field','createdat');
            $request->inputSet('order','desc');
        }

        $request->inputSet('isdel','0');
        return parent::select($request);
    }

    protected function formatNormal($items, $total): Response
    {
        foreach($items as &$val){
            $val['v1_plan_data'] = json_decode($val['v1_plan_data'], true);
            $val['v2_plan_data'] = json_decode($val['v2_plan_data'], true);
            $val['v3_plan_data'] = json_decode($val['v3_plan_data'], true);

            $val['level_reward_data'] = [$val['v1_plan_data'],$val['v2_plan_data'],$val['v3_plan_data']];

            if(empty($val['v1_plan_data']) && empty($val['v2_plan_data']) && empty($val['v3_plan_data'])){
                $val['level_reward_data'] = [];
            }else{
                $val['level_reward_data'] = [$val['v1_plan_data'],$val['v2_plan_data'],$val['v3_plan_data']];
            }
        }
        return parent::formatNormal($items, $total);
    }

    public function insert(Request $request): Response
    {
        $param = $request->all();
        if(!empty($param['level_reward_data'])){
            foreach ($param['level_reward_data'] as $val){
                if(strtoupper($val['user_level'])=='V1'){
                    $param['v1_plan_data'] = json_encode($val,256);
                }elseif(strtoupper($val['user_level'])=='V2'){
                    $param['v2_plan_data'] = json_encode($val,256);
                }elseif(strtoupper($val['user_level'])=='V3'){
                    $param['v3_plan_data'] = json_encode($val,256);
                }
            }
            $request->inputSet('v1_plan_data',$param['v1_plan_data']);
            $request->inputSet('v2_plan_data',$param['v2_plan_data']);
            $request->inputSet('v3_plan_data',$param['v3_plan_data']);
        }
        return parent::insert($request);
    }

    public function update(Request $request): Response
    {
        $param = $request->all();
        if(!empty($param['level_reward_data'])){
            foreach ($param['level_reward_data'] as $val){
                if(strtoupper($val['user_level'])=='V1'){
                    $param['v1_plan_data'] = json_encode($val,256);
                }elseif(strtoupper($val['user_level'])=='V2'){
                    $param['v2_plan_data'] = json_encode($val,256);
                }elseif(strtoupper($val['user_level'])=='V3'){
                    $param['v3_plan_data'] = json_encode($val,256);
                }
            }
            $request->inputSet('v1_plan_data',$param['v1_plan_data']);
            $request->inputSet('v2_plan_data',$param['v2_plan_data']);
            $request->inputSet('v3_plan_data',$param['v3_plan_data']);
        }
        return parent::update($request);
    }

    public function delete(Request $request): Response
    {
        return parent::isDelete($request);
    }

    public function viewDetail(Request $request): Response
    {
        $param = $request->all();
        $detail = $this->model->find($param['id']);
        $detail['v1_plan_data'] = $detail['v1_plan_data'] ? json_decode($detail['v1_plan_data'],true):[];
        $detail['v2_plan_data'] = $detail['v2_plan_data'] ? json_decode($detail['v2_plan_data'],true):[];
        $detail['v3_plan_data'] = $detail['v3_plan_data'] ? json_decode($detail['v3_plan_data'],true):[];

        $detail['level_reward_ploy'] = [$detail['v1_plan_data'],$detail['v2_plan_data'],$detail['v3_plan_data']];

        //默认方案
        $defaultDetail = $this->model->find(1);
        $defaultDetail['v1_plan_data'] = $defaultDetail['v1_plan_data'] ? json_decode($defaultDetail['v1_plan_data'],true):[];
        $defaultDetail['v2_plan_data'] = $defaultDetail['v2_plan_data'] ? json_decode($defaultDetail['v2_plan_data'],true):[];
        $defaultDetail['v3_plan_data'] = $defaultDetail['v3_plan_data'] ? json_decode($defaultDetail['v3_plan_data'],true):[];

        $defaultDetail['level_reward_ploy'] = [$defaultDetail['v1_plan_data'],$defaultDetail['v2_plan_data'],$defaultDetail['v3_plan_data']];

        $data = [];
        $i=1;
        foreach(['V3','V2','V1'] as $val){
            foreach(['V3','V2','V1'] as $v){
                $data[$val.'_'.$v] = [
                    "id"=>$i++,

                    "level1"=>$val,
                    "talent"=>$v,

                    "talent_rate"=>0,
                    "level1_rate"=>0,
                    "total_rate"=>0,
                ];
            }
        }

        foreach ($detail['level_reward_ploy'] as $val){
            foreach(['V3','V2','V1'] as $level1){
                $talent = $val['user_level'];
                $data[$level1.'_'.$talent]['talent_rate'] = $val['talent_rate'];
                $data[$level1.'_'.$talent]['level1_rate'] = $val['level1'.'_'.strtolower($level1)];
                $data[$level1.'_'.$talent]['total_rate'] = $val['talent_rate']+$val['level1'.'_'.strtolower($level1)];
            }
        }

        //该方案的用户是直推
        if($param['id']>1 && $param['type']==0){
            foreach ($defaultDetail['level_reward_ploy'] as $val){
                foreach(['V3','V2','V1'] as $level1){
                    $talent = $val['user_level'];
                    $data[$level1.'_'.$talent]['level1_rate'] = 0;
                    $data[$level1.'_'.$talent]['total_rate'] = $data[$level1.'_'.$talent]['talent_rate'] +
                        $data[$level1.'_'.$talent]['level1_rate'];

                }
            }
        }elseif($param['id']>1 && $param['type']==1){
            foreach ($defaultDetail['level_reward_ploy'] as $val){
                foreach(['V3','V2','V1'] as $level1){
                    $talent = $val['user_level'];
                    $data[$level1.'_'.$talent]['talent_rate'] = $val['talent_rate'];
                    $data[$level1.'_'.$talent]['total_rate'] = $data[$level1.'_'.$talent]['talent_rate'] +
                        $data[$level1.'_'.$talent]['level1_rate'];

                }
            }
        }

        return $this->json(0,'ok',$data);
    }
}